package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.DeleteDTO;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.util.DataUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.BomBillDao;
import com.xbongbong.saas.domain.entity.BomProductEntity;
import com.xbongbong.saas.domain.entity.ext.BomBillEntityExt;
import com.xbongbong.saas.domain.entity.ext.ProductEntityExt;
import com.xbongbong.saas.enums.OperatePageEnum;
import com.xbongbong.saas.enums.RelativeProductEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.UpdateDataTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.BomBillEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.product.ProductionOrderFinishProductEnum;
import com.xbongbong.saas.enums.product.ProductionOrderMaterielProductEnum;
import com.xbongbong.saas.help.ExplainHelp;
import com.xbongbong.saas.help.SaasFormHelp;
import com.xbongbong.saas.help.ShowFormatHelp;
import com.xbongbong.saas.model.BomBillModel;
import com.xbongbong.saas.model.BomBillSubDataModel;
import com.xbongbong.saas.model.BomProductModel;
import com.xbongbong.saas.model.PaasFormSubDataModel;
import com.xbongbong.saas.model.ProductModel;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.apache.commons.collections4.MapUtils;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getDoubleOrDefaultFromFormData;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/1/23 10:53
 * @since v1.0
 */
@Service("bomBillModel")
public class BomBillModelImpl implements BomBillModel {

    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private BomBillDao bomBillDao;
    @Resource
    private PaasFormSubDataModel paasFormSubDataModel;
    @Resource
    private BomBillSubDataModel bomBillSubDataModel;
    @Resource
    private ExplainHelp explainHelp;
    @Resource
    private ShowFormatHelp showFormatHelp;
    @Resource
    private ProductModel productModel;
    @Resource
    private BomProductModel bomProductModel;
    @Resource
    private SaasFormHelp saasFormHelp;

    @Override
    public Integer insert(BomBillEntityExt entity) throws XbbException {
        //TODO  先进行数据库的插入动作
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        paasFormSubDataModel.transferSubForm(entity, IndexTypeEnum.IDX_SAAS_BOM_SUB);
        StandardizeDataUtil.standardizeNotString(entity);
        Integer insert = bomBillDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_BOM);
        paasEsModel.insert(insertDTO);
        paasFormSubDataModel.insertSubFormData(entity, IndexTypeEnum.IDX_SAAS_BOM_SUB);
        return insert;
    }

    @Override
    public Integer update(BomBillEntityExt entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        paasFormSubDataModel.transferSubForm(entity, IndexTypeEnum.IDX_SAAS_BOM_SUB);
        StandardizeDataUtil.standardizeNotString(entity);
        Integer success = bomBillDao.update(entity);
        Integer updateSub = paasFormSubDataModel.updateSubFormData(entity, IndexTypeEnum.IDX_SAAS_BOM_SUB);
        if (Objects.equals(1, success) || Objects.equals(1, updateSub)) {
            // 更新时间
            success = bomBillDao.updateTime(now + 1, entity.getId(), entity.getCorpid());
            // 更新es
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entity, paasFormDataEntity, false);
            paasFormDataEntity.setCreatorId(null);
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_BOM);
            insertDTO.getSource().remove("creatorId");
            paasEsModel.update(insertDTO);

        }
        if (Objects.nonNull(entity.getSubFormData()) && !entity.getSubFormData().isEmpty()) {
            entity.getData().putAll(entity.getSubFormData());
        }
        return success;
    }

    @Override
    public List<BomBillEntityExt> findEntitys(Map<String, Object> param) {
        List<BomBillEntityExt> entitys = bomBillDao.findEntitys(param);
        Object corpid = param.get(ParameterConstant.CORPID);
        if (Objects.nonNull(corpid)) {
            paasFormSubDataModel.findSubEntity(corpid.toString(), entitys, IndexTypeEnum.IDX_SAAS_BOM_SUB);
        }
        return entitys;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return bomBillDao.getEntitysCount(param);
    }

    @Override
    public BomBillEntityExt getByKey(Long key, String corpid) {
        BomBillEntityExt entityExt = bomBillDao.getByKey(key, corpid);
        paasFormSubDataModel.findSubEntity(corpid, Collections.singletonList(entityExt), IndexTypeEnum.IDX_SAAS_BOM_SUB);
        return entityExt;
    }

    @Override
    public Integer deleteBatch(List<Long> bomIdIn, String corpid) throws XbbException {
        Integer integer = bomBillDao.deleteBatch(bomIdIn, corpid);
        paasEsModel.deleteBatch(bomIdIn, corpid, DelEnum.DELETE.getDel(), IndexTypeEnum.IDX_SAAS_BOM);
        paasFormSubDataModel.deleteBatchByDataId(bomIdIn, corpid, IndexTypeEnum.IDX_SAAS_BOM_SUB, DelEnum.DELETE_SUBFORM);
        return integer;
    }

    @Override
    public Integer deleteByKey(Long key,String corpid) throws XbbException{
        DeleteDTO deleteDTO = new DeleteDTO();
        deleteDTO.setEsId(corpid+"_"+key);
        deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_BOM);
        paasEsModel.delete(deleteDTO);
        Integer integer = bomBillDao.deleteByKey(key, corpid);
        paasFormSubDataModel.deleteByDataId(key, corpid, IndexTypeEnum.IDX_SAAS_BOM_SUB);
        return integer;
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> list, String corpid) throws XbbException {
        //1. 先拿到子表单数据
        List<UpdateDataEntity> subList = DataUtil.getUpdateSubDataList(list);
        // 2. 主表删掉子表单数据
        DataUtil.removeSubDataInData(list);
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(list)) {
            StandardizeDataUtil.standardizeList(list);
            integer = bomBillDao.updateBatch(list, corpid);
            paasEsModel.updateBatchMuchField(list, IndexTypeEnum.IDX_SAAS_BOM);
        }
        if (!subList.isEmpty()) {
            bomBillSubDataModel.updateBatch(subList, corpid);
        }
        return integer;
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> updateList, String corpid, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        //1. 先拿到子表单数据
        List<UpdateDataEntity> subList = DataUtil.getUpdateSubDataList(updateList);
        // 2. 主表删掉子表单数据
        DataUtil.removeSubDataInData(updateList);
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(updateList)) {
            StandardizeDataUtil.standardizeList(updateList);
            integer = bomBillDao.updateBatch(updateList, corpid);
            paasEsModel.updateBatchMuchField(updateList, IndexTypeEnum.IDX_SAAS_BOM, refreshPolicy);
        }
        // todo
        if (!subList.isEmpty()) {
            bomBillSubDataModel.updateBatch(subList, corpid);
        }
        return integer;
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return bomBillDao.findEntitys(param);
    }

    @Override
    public void formatAddExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        List<FieldAttrEntity> explainList = formatExplainDTO.getExplainList();
        String corpid = formatExplainDTO.getCorpid();
        for (FieldAttrEntity fieldAttrEntity : explainList) {
            Integer fieldType = fieldAttrEntity.getFieldType();
            if (Objects.equals(fieldType, FieldTypeEnum.SELECT_PRODUCT.getType())) {
                fieldAttrEntity.setSupportScanning(BasicConstant.ONE);
                explainHelp.formatSelectProductExplains(fieldAttrEntity, XbbRefTypeEnum.BOM_BILL, corpid, OperatePageEnum.NEW.getCode(), false);
            }
        }
    }

    @Override
    public void formatUpdateExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        formatAddExplain(formatExplainDTO);
    }

    /**
     * Bom清单编辑格式化数据格式
     *
     * @param formDataUpdateGetDTO saas编辑数据
     * @param result               要返回的jsonobject
     * @throws XbbException 参数格式错误异常
     * @author long.rao
     * @date 2019-01-29 21:19
     */
    @Override
    public void formatData(SaasAnalysisDataDTO formDataUpdateGetDTO, JSONObject result) throws XbbException {
        // 格式化bom版本
        showFormatHelp.formatTextCheck4Show(result, BomBillEnum.VERSION.getAttr(), BomBillEnum.VERSION_LINK_TEXT.getAttr());
        // 格式化bom关联产品
//        formatProduct4Show(result, formDataUpdateGetDTO);
    }

    /**
     * 获取bomProductList
     * @param corpid
     * @param bomProductList
     * @param productArray
     * @param productMap
     * @param type 1 成品 2 物料
     * @throws XbbException
     */
    @Override
    public void formatProductList(String corpid, List<BomProductEntity> bomProductList, JSONArray productArray, Map<Long, ProductEntityExt> productMap, Integer type) throws XbbException {
        Integer sort = 0;
        for (int i = 0; i < productArray.size(); i++) {
            JSONObject jsonObject = productArray.getJSONObject(i);
            sort++;
            // 页面传入的产品id
            Long pageProductId = FastJsonHelper.getLongOrDefaultFromFormData(jsonObject, StringConstant.SAAS_LINK_BUSINESS_ID, 0L);
            // 产品数量
            Double productNum = getDoubleOrDefaultFromFormData(jsonObject, RelativeProductEnum.NUM.getSaasAttr(), 0D);
            // 损耗率
            Double attritionRate = getDoubleOrDefaultFromFormData(jsonObject, RelativeProductEnum.ATTRITION_RATE.getSaasAttr(), 0D);
            // 备注
            String memo = FastJsonHelper.getStringOrDefaultFromFormData(jsonObject, RelativeProductEnum.MEMO.getSaasAttr(), "");
            if (productMap.containsKey(pageProductId)) {
                ProductEntityExt product = productMap.get(pageProductId);
                JSONObject productObject = product.getData();
                String specification = FastJsonHelper.getStringOrDefaultFromFormData(productObject, ProductEnum.SPECIFICATION_LINK_TEXT.getAttr(), "");
                String productName = FastJsonHelper.getStringOrDefaultFromFormData(productObject, ProductEnum.NAME.getAttr(), "");
                String productNo = FastJsonHelper.getStringOrDefaultFromFormData(productObject, ProductEnum.PRODUCT_NO.getAttr(), "");
                String unit = FastJsonHelper.getStringOrDefaultFromFormData(productObject, ProductEnum.UNIT.getAttr(), "");
                String barcode = FastJsonHelper.getStringOrDefaultFromFormData(productObject, ProductEnum.BARCODE.getAttr(), "");
                Long parentId = FastJsonHelper.getLongOrDefaultFromFormData(productObject, ProductEnum.PARENT_ID.getAttr(), 0L);
                BomProductEntity productEntity = new BomProductEntity();
                productEntity.setBarcode(barcode);
                productEntity.setType(type);
                productEntity.setCorpid(corpid);
                productEntity.setProductId(pageProductId);
                productEntity.setParentId(parentId);
                productEntity.setProductNum(productNum);
                productEntity.setProductName(productName);
                productEntity.setProductNo(productNo);
                productEntity.setProductUnit(unit);
                productEntity.setProductSpecification(specification);
                productEntity.setSort(sort);
                productEntity.setMemo(memo);
                //JSONObject productJsonObject = new JSONObject();
                if (Objects.equals(type, 2)) {
                    productEntity.setAttritionRate(attritionRate);
                    //productJsonObject.put(BomMaterielProductEnum.LOSS_RATE.getAttr(), attritionRate);
                }
                /*productJsonObject.put(BomMaterielProductEnum.PRODUCT.getAttr(), pageProductId);
                productJsonObject.put(BomMaterielProductEnum.UNIT.getAttr(), unit);
                productJsonObject.put(BomMaterielProductEnum.NUM.getAttr(), productNum);
                productJsonObject.put(BomMaterielProductEnum.MEMO.getAttr(), memo);
                productEntity.setData(productJsonObject);*/
                bomProductList.add(productEntity);
            }
        }
    }

    @Override
    public void formatProduct4Show2(Long dataId, String corpid, JSONObject data, UpdateDataTypeEnum updateDataTypeEnum) throws XbbException {
        // 获取关联产品
        List<BomProductEntity> bomProductEntityList = new ArrayList<>();
        if (Objects.isNull(dataId) || Objects.equals(dataId, 0L)) {
            if (data.get(BomBillEnum.PRODUCT.getAttr()) instanceof JSONObject) {
                // 获取成品产品列表
                JSONArray productArray = FastJsonHelper.getJsonArrOrDefaultFromFormData(FastJsonHelper.getJsonObjectOrDefaultFromFormData(data, BomBillEnum.PRODUCT.getAttr(), new JSONObject()), StringConstant.PRODUCT_LIST, new JSONArray());
                Map<Long, ProductEntityExt> productMap = productModel.getProductMap(corpid, productArray);
                formatProductList(corpid, bomProductEntityList, productArray, productMap, 1);
                // 获取物料产品列表
                JSONArray materielArray = FastJsonHelper.getJsonArrOrDefaultFromFormData(FastJsonHelper.getJsonObjectOrDefaultFromFormData(data, BomBillEnum.MATERIEL.getAttr(), new JSONObject()), StringConstant.PRODUCT_LIST, new JSONArray());
                Map<Long, ProductEntityExt> materielMap = productModel.getProductMap(corpid, materielArray);
                formatProductList(corpid, bomProductEntityList, materielArray, materielMap, 2);
            } else {
                JSONArray finishArray = data.getJSONArray(BomBillEnum.PRODUCT.getAttr());
                if (Objects.nonNull(finishArray)) {
                    for (int i = 0; i < finishArray.size(); i++) {
                        JSONObject jsonObject = finishArray.getJSONObject(i);
                        BomProductEntity bomProductEntity = new BomProductEntity();
                        Long subProductId = FastJsonHelper.getLongFromFormData(jsonObject, BusinessConstant.PRODUCT_SUB_ID);
                        if (Objects.nonNull(subProductId)) {
                            bomProductEntity.setId(subProductId);
                        }
                        bomProductEntity.setType(1);
                        bomProductEntity.setData(jsonObject);
                        bomProductEntityList.add(bomProductEntity);
                    }
                }
                JSONArray materialArray = data.getJSONArray(BomBillEnum.MATERIEL.getAttr());
                if (Objects.nonNull(materialArray)) {
                    for (int i = 0; i < materialArray.size(); i++) {
                        JSONObject jsonObject = materialArray.getJSONObject(i);
                        BomProductEntity bomProductEntity = new BomProductEntity();
                        Long subProductId = FastJsonHelper.getLongFromFormData(jsonObject, BusinessConstant.PRODUCT_SUB_ID);
                        if (Objects.nonNull(subProductId)) {
                            bomProductEntity.setId(subProductId);
                        }
                        bomProductEntity.setType(2);
                        bomProductEntity.setData(jsonObject);
                        bomProductEntityList.add(bomProductEntity);
                    }
                }
            }
        } else {
            // 检索del=0和del=6的数据，del=6是工作流中审批节点拒绝和撤回产生的数据，不是通过审批拒绝、撤回的数据不会产生del=6的数据，如果拒绝和撤回重新提交后，del=6将会被置回del=0
            bomProductEntityList = bomProductModel.getProductsByBomId(dataId, corpid,null, Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.APPROVE_FAILED.getDel()));
            bomProductEntityList.sort((o1, o2) -> {
                if (Objects.isNull(o1.getSort()) && Objects.isNull(o2.getSort())) {
                    return 0;
                }
                if (Objects.isNull(o1.getSort())) {
                    return 1;
                }
                if (Objects.isNull(o2.getSort())) {
                    return -1;
                }
                return o1.getSort().compareTo(o2.getSort());
            });
        }
        JSONArray productArray = new JSONArray();
        JSONArray materielProductarray = new JSONArray();
        for (BomProductEntity bomProductEntity : bomProductEntityList){
            if (Objects.equals(bomProductEntity.getType(),1)){
                JSONObject json = formatSingleProduct(bomProductEntity);
                if (updateDataTypeEnum.isUpdate()){
                    json.put(BusinessConstant.PRODUCT_SUB_ID,bomProductEntity.getId());
                    if (Objects.isNull(bomProductEntity.getId())) {
                        json.put(BusinessConstant.PRODUCT_SUB_ID,0);
                    }
                }else {
                    json.put(BusinessConstant.PRODUCT_SUB_ID,0);
                }
                if (updateDataTypeEnum.isLinkAdd()){
                    json.put(SelectProductEnum.UNIT.getAttr(), saasFormHelp.formatUnit(bomProductEntity.getCorpid(),bomProductEntity.getProductUnit()));
                    json.put(BusinessConstant.RELY_PRODUCT_SUB_ID, bomProductEntity.getId());
                    json.put(ProductEnum.ENABLE_MULTI_UNIT.getAttr(), BasicConstant.ONE);
                }
                productArray.add(json);
            }else {
                JSONObject json = bomProductEntity.getData();
                if (Objects.isNull(json) || json.isEmpty()){
                    json = new JSONObject();
                    for (ProductionOrderMaterielProductEnum productionOrderMaterielProductEnum : ProductionOrderMaterielProductEnum.values()){
                        String attr = productionOrderMaterielProductEnum.getAttr();
                        switch (productionOrderMaterielProductEnum){
                            case PRODUCT:
                                json.put(attr,bomProductEntity.getProductId());
                                break;
                            case NUM:
                                json.put(attr,bomProductEntity.getProductNum());
                                break;
                            case MEMO:
                                json.put(attr,bomProductEntity.getMemo());
                                break;
                            case LOSS_RATE:
                                json.put(attr,bomProductEntity.getAttritionRate());
                                break;
                            case UNIT:
                                json.put(attr,bomProductEntity.getProductUnit());
                                break;
                            default:
                                break;
                        }
                    }
                }
                if (updateDataTypeEnum.isUpdate()){
                    json.put(BusinessConstant.PRODUCT_SUB_ID,bomProductEntity.getId());
                    if (Objects.isNull(bomProductEntity.getId())) {
                        json.put(BusinessConstant.PRODUCT_SUB_ID,0);
                    }
                }else {
                    json.put(BusinessConstant.PRODUCT_SUB_ID,0);
                }
                if (updateDataTypeEnum.isLinkAdd()){
                    json.put(SelectProductEnum.UNIT.getAttr(), saasFormHelp.formatUnit(bomProductEntity.getCorpid(),bomProductEntity.getProductUnit()));
                    json.put(BusinessConstant.RELY_PRODUCT_SUB_ID, bomProductEntity.getId());
                    json.put(ProductEnum.ENABLE_MULTI_UNIT.getAttr(), BasicConstant.ONE);
                }
                json.put("refId",bomProductEntity.getId());
                materielProductarray.add(json);
            }
        }

        List<String> offlinedProducts = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (Objects.equals(updateDataTypeEnum, UpdateDataTypeEnum.COPY) && CollectionsUtil.isNotEmpty(bomProductEntityList)) {
            // BOM产品ID
            List<Long> productIdList = bomProductEntityList.stream().map(BomProductEntity::getProductId).collect(Collectors.toList());
            Map<Long, ProductEntityExt> productMap = productModel.getProductMapByIds(corpid, productIdList, DelEnum.NORMAL.getDel(),
                    Arrays.asList(StringConstant.SAAS_LINK_BUSINESS_ID,ProductEnum.getAttrConnectData(ProductEnum.NAME),ProductEnum.getAttrConnectData(ProductEnum.ONLINE)));
            // 下架产品剔除掉
            removeOnlineProduct(productMap, productArray, offlinedProducts);
            removeOnlineProduct(productMap, materielProductarray, offlinedProducts);
            if (CollectionsUtil.isEmpty(productArray)) {
                productArray = new JSONArray();
                JSONObject json = new JSONObject();
                json.put(SelectProductEnum.NUM.getAttr(), 0);
                productArray.add(json);
            }
        }
        data.put(BasicConstant.OFF_LINED_PRODUCTS, offlinedProducts);
        data.put(BomBillEnum.PRODUCT.getAttr(),productArray);
        data.put(BomBillEnum.MATERIEL.getAttr(),materielProductarray);
    }

    @Override
    public JSONObject formatSingleProduct(BomProductEntity bomProductEntity) {
        JSONObject json = bomProductEntity.getData();
        if (Objects.equals(bomProductEntity.getType(),1)) {
            if (Objects.isNull(json) || json.isEmpty()) {
                json = new JSONObject();
                for (ProductionOrderFinishProductEnum productionOrderFinishProductEnum : ProductionOrderFinishProductEnum.values()) {
                    String attr = productionOrderFinishProductEnum.getAttr();
                    switch (productionOrderFinishProductEnum) {
                        case PRODUCT:
                            json.put(attr, bomProductEntity.getProductId());
                            break;
                        case NUM:
                            json.put(attr, bomProductEntity.getProductNum());
                            break;
                        case UNIT:
                            json.put(attr, bomProductEntity.getProductUnit());
                            break;
                        case MEMO:
                            json.put(attr, bomProductEntity.getMemo());
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        return json;
    }


    @Override
    public Integer updateTime(Long id,String corpid) throws XbbException {
        long now = DateUtil.getInt();
        // 更新时间
        Integer success = bomBillDao.updateTime(now, id, corpid);
        // 更新es
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        paasFormDataEntity.setId(id);
        paasFormDataEntity.setDataId(id);
        paasFormDataEntity.setCorpid(corpid);
        paasFormDataEntity.setUpdateTime(now);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_BOM);
        paasEsModel.update(insertDTO);
        return success;
    }

    /**
     * 移除下架的产品
     *
     * @param productMap       产品map
     * @param productArray     需要检查是否移除的产品json集合
     * @param offlinedProducts 移除的产品名字
     */
    private void removeOnlineProduct(Map<Long, ProductEntityExt> productMap, JSONArray productArray, List<String> offlinedProducts) {
        if (CollectionsUtil.isEmpty(productArray) || Objects.isNull(offlinedProducts) || MapUtils.isEmpty(productMap)) {
            return ;
        }
        productArray.removeIf(product -> {
            JSONObject bomproductData = product instanceof JSONObject ? (JSONObject) product : (JSONObject) JSONObject.toJSON(product);
            Long productId = bomproductData.getLong(ProductEnum.NAME.getAttr());
            ProductEntityExt productEntityExt = productMap.getOrDefault(productId, new ProductEntityExt());
            JSONObject productData = productEntityExt.getData();
            if (Objects.nonNull(productData) && Objects.equals(productData.getInteger(ProductEnum.ONLINE.getAttr()), 0)) {
                offlinedProducts.add(productData.getString(ProductEnum.NAME.getAttr()));
                return true;
            }
            return false;
        });
    }


}
